#include "QxNavigateWidget.hpp"
#include "ui_QxNavigateWidget.h"

#include <QStackedWidget>

class QxNavigateWidget::QxNavigateWidgetPrivate
{
public:

  QxNavigateWidgetPrivate( QxNavigateWidget* me ) : m_self( me ), m_stackedWidget( 0 ) {
    m_ui.setupUi( m_self );
    enableBackward( false );
  }

  void setTitle( const QString& title ) {
    m_ui.txtTitle->setText( title );
  }

  void enableBackward( bool bFlag ) {
    m_ui.backwardBtn->setVisible( bFlag );
  }

  QxNavigateWidget*         m_self;
  Ui::QxNavigateWidget      m_ui;

  QStackedWidget*           m_stackedWidget;
};

QxNavigateWidget::QxNavigateWidget( QWidget* parent )
  : QWidget( parent ), _pd( new QxNavigateWidgetPrivate( this ) )
{
}

QxNavigateWidget::~QxNavigateWidget()
{
}

void QxNavigateWidget::attachStackedWidget( QStackedWidget* stackedWidget )
{
  _pd->m_stackedWidget = stackedWidget;
  _pd->setTitle( _pd->m_stackedWidget->currentWidget()->windowTitle() );
  connect( stackedWidget, SIGNAL( currentChanged ( int ) ), this, SLOT( slotIndexChanged( int ) ) );
}

void QxNavigateWidget::slotIndexChanged( int idx )
{
  _pd->setTitle( _pd->m_stackedWidget->currentWidget()->windowTitle() );
  _pd->enableBackward( idx );
}

void QxNavigateWidget::on_backwardBtn_clicked()
{
  if( _pd->m_stackedWidget ) {
    _pd->m_stackedWidget->setCurrentIndex( 0 );
  }
}
